package com.pks.datacenter;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pks.datacenter.config.MyConfig;
import com.pks.resultful.common.SRemoteResult;
import com.pks.util.UtilCollection;
import com.pks.web.dto.ServiceResult;
import com.pks.web.springmvc.controller.BasicController;
import com.pks.web.springmvc.support.FileSupport;

@RestController
@RequestMapping("/file")
@Scope("prototype")
@Component("fileController")
public class FileController extends BasicController {


	private static final String DEFAULT_FOLDER = "temp";
	
	public static final List<String> kzx_suffix = new ArrayList<String>();
	public static final List<String> img_suffix = new ArrayList<String>();
	static {
		kzx_suffix.add("com");
		kzx_suffix.add("exe");
		kzx_suffix.add("bat");
		kzx_suffix.add("lnk");
		kzx_suffix.add("jsp");
		kzx_suffix.add("html");
		kzx_suffix.add("dll");
		kzx_suffix.add("drv");
		kzx_suffix.add("sys");
		kzx_suffix.add("pif");

		img_suffix.add("bmp");
		img_suffix.add("jpg");
		img_suffix.add("jpeg");
		img_suffix.add("png");
		img_suffix.add("gif");
	}

//	// FastDFS上传文件（saveInfo：是否通知知识中心保存资源信息）
//	@RequestMapping("/fupload.ajax")
//	public void fupload(HttpServletRequest request, boolean saveInfo, PrintWriter out) throws Exception {
//		final String from = SystemContext.getSite();
//		ServiceResult sr = uploadFileFastDFS(request, Configuration.getFastDFSIpPorts(), null,Configuration.getFastdfsGroupName());
//		if (sr.success()) {
//			Object d = sr.getD();
//			if (d instanceof UploadSuccessResult) {
//				UploadSuccessResult res = (UploadSuccessResult) d;
//				User user = WebContext.getLoginUser(request);
//
//				RemoteKresource rk = new RemoteKresource();
//				rk.initBaseInfo(FilenameUtils.getBaseName(res.getOriginalFilename()), null, WebContext.getLoginUserId(request),
//						user.getRealname(), user.getDeptId(), user.getShow_deptName(), new Date(), from,
//						String.valueOf(UtilString.getLongUUID()), res.getSavePath());
//
//				if (saveInfo) {
//					jmsHandler.send(ServiceResult.GSON_DT.toJson(rk));
//				}
//				rk.setSuffix(RemoteKresource.encode(res.getSuffix()));
//				sr.setD(rk);
//			} else {
//				sr = ServiceResult.initError("上传失败");
//			}
//		}
//		out.write(ServiceResult.GSON_DT.toJson(sr));
//	}
//
//	// 文件上传
//	@RequestMapping("/upload.ajax")
//	public void upload(String type, HttpServletRequest request, PrintWriter out) throws Exception {
//		if (StringUtils.isBlank(type)) {
//			type = DEFAULT_FOLDER;
//		}
//		ServiceResult sr = FileSupport.uploadFile(request, type, Configuration.getRealPath(),
//				UtilCollection.initHashSet("/upload/.*", "/" + DEFAULT_FOLDER + ".*", "/site/.*"), false,
//				UtilCollection.initHashSet(UtilFile.dangerFileTpye));
//		out.write(ServiceResult.GSON_DT.toJson(sr));
//	}

	/**
	 * 文件下载
	 * 
	 * @param path
	 *            相对项目的路径，如"/download/test.xls"
	 * @param fileName
	 *            保存的文件名，带文件后缀，可以是中文，如"用户信息.xls"
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/download")
	public SRemoteResult<String> download(String path, String fileName, HttpServletRequest request, HttpServletResponse response) {
//		FileSupport.downloadFile(response, Configuration.getRealPath(), path, fileName,UtilCollection.initHashSet("/upload/.*", "/" + DEFAULT_FOLDER + ".*", "/site/.*","/exportfile/.*"));
		ServiceResult downloadFile = FileSupport.downloadFile(response, MyConfig.getSavePath(), path, fileName+".xls", UtilCollection.initHashSet("/dbfield/.*"));
		return SRemoteResult.initSuccess(null);
	}

}
